//
//  ALNewFeatureCollectionViewController.swift
//  Swift01
//
//  Created by Miaoyang on 16/4/12.
//  Copyright © 2016年 Miaoyang. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ALNewFeatureCollectionViewController: UICollectionViewController {

    //拿到layouts改变item大小
    let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init(){
        super.init(collectionViewLayout: layout)
        //设置相关属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.collectionView!.registerClass(ALNewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }
    


   

    // MARK: UICollectionViewDataSource

    


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                return NumberOfNewFeatureItem
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ALNewFeatureCollectionViewCell
        // 设置cell的index,告诉他显示的是第几页
        cell.index = indexPath.item

        return cell
    }

 

}
//延展
extension ALNewFeatureCollectionViewController{
    // scrollView停止滚动时调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView){
        
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width)
        if index == NumberOfNewFeatureItem - 1 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            // 最后一页,让cell开始动画
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! ALNewFeatureCollectionViewCell
            cell.startAnimation()
    }
}

class ALNewFeatureCollectionViewCell: UICollectionViewCell {
    //设置一个属性用来记录当前页码
    var index:Int = 0{
        didSet {
            //根据下标设置图片
            let imageName = "new_feature_\(index + 1)"
            bkgImageView.image = UIImage(named: imageName)
            // 隐藏按钮
            startButton.hidden = true
            
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加背景图
        prepareUI()
    }
    
//    private func updaBackgroudImageView(){
//        //添加背景图,当View是Cell的时候添加view需要添加到contentView上
//        contentView.addSubview(bkgImageView)
//        //设置约束
//        bkgImageView.snp_makeConstraints { (make) in
//            make.edges.equalTo(UIEdgeInsetsZero)
//        }
//        
//    }
    private func prepareUI() {
        // 1.添加控件
        // 当是cell的时候,view都添加到contentView
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        // 2.添加约束
        // 背景
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 开始按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    // MARK: - 动画方法
    func startAnimation() {
        // 显示按钮
        startButton.hidden = false
        
        // 将transform的缩放改成0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            self.startButton.transform = CGAffineTransformIdentity  // 清空transform的缩放, 1
        }) { (_) -> Void in
            print("动画完成了")
        }
    }
    

    //MARK:懒加载背景
    private lazy var bkgImageView:UIImageView  = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startButton:UIButton = {
        
     let button = UIButton()
        //设置图片
        button.setBackgroundImage(UIImage(named:"new_feature_finish_button"), forState: UIControlState.Normal)
        //设置文字一些属性
       button.setBackgroundImage(UIImage(named:"new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor() ,forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(ALNewFeatureLabelFontSize)
        button.sizeToFit()
        return button
        
    }()
}
}
